// 插件用于注册全局组件  可以用于注册全局指令

import XtxSkeleton from '@/components/skeleton/index.vue'
import XtxCarousel from '@/components/carousel/index.vue'
import XtxMore from './more/index.vue'
import Bread from './Bread/index.vue'
import BreadItem from './Bread/Item.vue'
import XtxCity from './city/index.vue'
import XtxNumbox from '@/components/numbox/index.vue'
import XtxButton from '@/components/button/index.vue'
import XtxCheckbox from '@/components/checkbox/index.vue'
// import XtxMessage from '@/components/message/message.vue'
import { useIntersectionObserver } from '@vueuse/core'
import errorImg from '@/assets/images/200.png'
// 这里用的是vue制定好的类型
import { App } from 'vue'

export default {
  // todo: 待会完善app类型
  // 这里的app 指的就是 Vue应用实例, 类型是固定的 且 确定的!!!  Vue提供好了 , 只需要导入使用即可
  install(app: App) {
    app.component('XtxSkeleton', XtxSkeleton)
    app.component(XtxCarousel.name, XtxCarousel)
    app.component(XtxMore.name, XtxMore)

    app.component('XtxBread', Bread)
    app.component('XtxBreadItem', BreadItem)
    app.component(XtxCity.name, XtxCity)
    app.component(XtxNumbox.name, XtxNumbox)
    app.component(XtxButton.name, XtxButton)
    app.component('XtxCheckbox', XtxCheckbox)
    // app.component('XtxMessage', XtxMessage)

    // <img v-lazy="https://yanxuan-item.nosdn.127.net/5b3c61cf75a2480774616f5662a5bccf.jpg" />
    app.directive('lazy', {
      // 指令的钩子函数
      // vue2  inserted updated
      // vue3  mounted
      mounted(el, { value }) {
        // console.log(el, value)
        // el.src = value
        const { stop } = useIntersectionObserver(el, ([{ isIntersecting }]) => {
          if (isIntersecting) {
            stop()
            // 图片进入可视区后 加载资源
            el.src = value

            // 图片地址出问题了... 兜底 注册error
            el.onerror = () => {
              el.src = errorImg
            }
          }
        })
      },
    })
  },
}
